import QtQuick 1.1
import com.nokia.meego 1.0

Page {
    Flickable {
        id: mainFlickable
        width: parent.width
        height: parent.height
        contentHeight: mainFlickableColumn.height
        Column {
            id: mainFlickableColumn
            width: parent.width
            spacing: 5
            Label {
                width: parent.width
                horizontalAlignment: Qt.AlignHCenter
                font.weight: Font.Bold
                text: "FTP Server"
            }
            SectionLabel {
            }
            Row {
                width: contentColumn.width
                anchors.horizontalCenter: parent.horizontalCenter
                Button {
                    id: startButton
                    width: parent.width / 2
                    text: qsTr("Start")
                    onClicked: {
                        outputEdit.text = ''
                        FtpServer.start()
                    }
                }
                Button {
                    id: stopButton
                    width: parent.width / 2
                    enabled: !startButton.enabled
                    text: qsTr("Stop")
                    onClicked: {
                        FtpServer.stop()
                    }
                }
            }
            SectionLabel {
            }
            Column {
                id: contentColumn
                //enabled: startButton.enabled
                anchors.horizontalCenter: parent.horizontalCenter
                spacing: 5
                Grid {
                    columns: 2
                    rows: 5
                    spacing: 5

                    Label {
                        id: addressLabel
                        anchors {
                            verticalCenter: addressEdit.verticalCenter
                            right: addressEdit.left
                            rightMargin: 10
                        }
                        text: qsTr("Address:")
                    }
                    TextField {
                        id: addressEdit
                        onTextChanged: {
                            Settings.setValue("serverAddress", addressEdit.text)
                        }
                    }

                    Label {
                        id: portLabel
                        anchors {
                            verticalCenter: portEdit.verticalCenter
                            right: portEdit.left
                            rightMargin: 10
                        }
                        text: qsTr("Port:")
                    }
                    TextField {
                        id: portEdit
                        onTextChanged: {
                            Settings.setValue("serverPort", portEdit.text)
                        }
                    }

                    Label {
                        id: rootDirLabel
                        anchors {
                            verticalCenter: rootDirEdit.verticalCenter
                            right: rootDirEdit.left
                            rightMargin: 10
                        }
                        text: qsTr("Root Directory:")
                    }
                    TextField {
                        id: rootDirEdit
                        onTextChanged: {
                            Settings.setValue("serverRootDir", rootDirEdit.text)
                        }
                    }

                    Label {
                        id: usernameLabel
                        anchors {
                            verticalCenter: usernameEdit.verticalCenter
                            right: usernameEdit.left
                            rightMargin: 10
                        }
                        text: qsTr("Login Name:")
                    }
                    TextField {
                        id: usernameEdit
                        onTextChanged: {
                            Settings.setValue("serverUsername", usernameEdit.text)
                        }
                    }

                    Label {
                        id: userpassLabel
                        anchors {
                            verticalCenter: userpassEdit.verticalCenter
                            right: userpassEdit.left
                            rightMargin: 10
                        }
                        text: qsTr("Login Password:")
                    }
                    TextField {
                        id: userpassEdit
                        onTextChanged: {
                            Settings.setValue("serverUserpass", userpassEdit.text)
                        }
                    }
                }
                CheckBox {
                    id: anonymousCheckbox
                    text: 'Allow read-only anonymous login'
                    onCheckedChanged: {
                        Settings.setValue("serverAllowAnonymous", anonymousCheckbox.checked)
                    }
                }
                CheckBox {
                    id: tlsCheckbox
                    text: 'Enable SFTP (TLS/SSLv3 encryption)'
                    function validate() {
                        if (tlsCheckbox.checked) {
                            if (FtpServer.hasPemCertificate()) {
                                outputEdit.text = qsTr('Valid PEM-certificate found. SFTP ready :)')
                            } else {
                                tlsCheckbox.checked = false
                                outputEdit.text = qsTr('Failed to create the PEM-certificate needed for SFTP. Are you sure OpenSSL is installed?')
                                return false
                            }
                        }
                        return true
                    }
                    onCheckedChanged: {
                        if (tlsCheckbox.validate())
                            Settings.setValue("serverTls", tlsCheckbox.checked)
                    }
                }
                CheckBox {
                    id: onlineCheckbox
                    text: 'Stay online while running'
                    onCheckedChanged: {
                        FtpServer.autoOnline = onlineCheckbox.checked
                    }
                }
            }
            SectionLabel {
            }
            ThemedTextArea {
                id: outputEdit
                text: ''
                visible: text.length > 0
                readOnly: true
                wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                font.pointSize: addressLabel.font.pointSize
                //font.weight: Font.Bold
                width: parent.width
            }
        }
    }

    ScrollDecorator {
        flickableItem: mainFlickable
    }

    Component.onCompleted: {
        addressEdit.text = Settings.value("serverAddress", "0.0.0.0")
        portEdit.text = Settings.value("serverPort", "2121")
        rootDirEdit.text = Settings.value("serverRootDir", "~/MyDocs")
        usernameEdit.text = Settings.value("serverUsername", "user")
        userpassEdit.text = Settings.value("serverUserpass", Settings.randomPassword(6))
        anonymousCheckbox.checked = Settings.value("serverAllowAnonymous", false)
        onlineCheckbox.checked = Settings.value("autoOnline", FtpServer.autoOnline)

        tlsCheckbox.checked = Settings.value("serverTls", false)
        if (!tlsCheckbox.validate())
            Settings.setValue("serverTls", false)

        FtpServer.started.connect( function() {
            startButton.enabled = false
            outputEdit.text += qsTr('FTP Server running') + "\n"
        } )
        FtpServer.stopped.connect( function() {
            startButton.enabled = true
            outputEdit.text += qsTr('FTP Server stopped') + "\n"
        } )
        FtpServer.output.connect( function(message) {
            outputEdit.text += message + "\n"
        } )
    }
}
